package com.starrk.myblog.foreground.config;

import com.starrk.myblog.foreground.mapper.properties.RedisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

@Configuration
public class RedisClusterConfig {
    @Autowired
    private RedisProperties redisProperties;

    @Bean
    public JedisCluster getJedisCluster(){
        String [] serverArray = redisProperties.getNodes().split(",");
        Set<HostAndPort> nodes = new HashSet<>();
        for(String hap:serverArray){
            String [] ipport = hap.split(":");
            nodes.add(new HostAndPort(ipport[0].trim(),Integer.valueOf(ipport[1].trim())));
        }
        return new JedisCluster(nodes,redisProperties.getTimeout(),redisProperties.getMaxAttemps());
//        return new JedisCluster(new HostAndPort("192.168.175.129",7001));
    }


}
